function [ output_args ] = uigridcontainer_ResizeFcn( h, e )
%UIGRIDCONTAINER   Resize function
%
%   Add some behaviour of the GRID-class from Microsofts .Net 4.0 Framework. 
%   With these function the grid can be splitted in three different ways.
%       1. Relative
%       2. Absolute
%       3. Automatic
%
%   Therefore 2 properties have to be added to the UIGRIDCONTAINER object by 
%   the SETAPPDATA function.
%
%       fig= figure;
%       h= uigridcontainer('v0', 'gridsize',[2 2],'parent',fig);
%       setappdata(h, 'rowdefinitions', CollectionOfLength);
%       setappdata(h, 'columndefinitions', CollectionOfLength);
%
%   COLLECTIONOFLENGTH is a one dimentional vector
%
%       CollectionOfLength= [100 NaN];
%
%   which allows: 
%        RELATIVE spacing by setting the elment value less then 1
%        ABSOLUTE pixelised spacing by setting the elment value grather then 1
%       AUTOMATIC spacing by setting the elment value to NaN
%
%   at last the resize function of the UIGRIDCONTAINER object have to be 
%   redirected.
%   
%       setappdata(h, 'resizefcn', get(h, 'resizefcn'));
%       set(h, 'resizefcn', @uigridcontainer_ResizeFcn);
%
%   Complete Example:
%       fig= figure;
%       h= uigridcontainer('v0', 'gridsize',[2 2],'parent',fig);
%       CollectionOfLength= [100 NaN];
%       setappdata(h, 'rowdefinitions', CollectionOfLength);
%       setappdata(h, 'columndefinitions', CollectionOfLength);
%       setappdata(h, 'resizefcn', get(h, 'resizefcn'));
%       set(h, 'resizefcn', @uigridcontainer_ResizeFcn);
%       uicontrol('string','1','parent', h);
%       uicontrol('string','2','parent', h);
%       uicontrol('string','3','parent', h);
%       uicontrol('string','4','parent', h);
% 

%   E. Olak 14-01-13

h_parent= get(h, 'parent');
parent_unit= get(h_parent, 'units');
set(h_parent, 'units', 'pixels');
parent_position= get(h_parent, 'position');
parent_width= parent_position(3);
parent_hight= parent_position(4);
set(h_parent, 'units', parent_unit);

h_rowdefinitions= getappdata(h, 'rowdefinitions');
h_columndefinitions= getappdata(h, 'columndefinitions');
h_VerticalWeight= ParseGridLength(h_rowdefinitions, parent_hight);
h_HorizontalWeight=ParseGridLength(h_columndefinitions, parent_width);
set(h, 'VerticalWeight', h_VerticalWeight);
set(h, 'HorizontalWeight', h_HorizontalWeight);

end

function Weights= ParseGridLength(CollectionOfGridLengths, AvailableSpaceAbsolute)

Weights=zeros(size(CollectionOfGridLengths));
RemainingSpaceRelative=1;
NumverOfAutoSpacing=0;
RelativeAllocatedSpace=0;

% Parse absolute spacings
for i= 1:numel(CollectionOfGridLengths)
    if CollectionOfGridLengths(i) > 1
        Weights(i)= CollectionOfGridLengths(i)/AvailableSpaceAbsolute;
        RemainingSpaceRelative= RemainingSpaceRelative- Weights(i);
    end
end

% Parse relative spacings and count automatic spacing 
for i= 1:numel(CollectionOfGridLengths)
    switch true
        case isnan(CollectionOfGridLengths(i)), 
            NumverOfAutoSpacing= NumverOfAutoSpacing+1;
        case CollectionOfGridLengths(i) <= 1,
            Weights(i)= RemainingSpaceRelative*CollectionOfGridLengths(i);
            RelativeAllocatedSpace = RelativeAllocatedSpace + Weights(i);
    end
end
RemainingSpaceRelative= RemainingSpaceRelative- RelativeAllocatedSpace;

% Parse  automatic spacing 
for i= 1:numel(CollectionOfGridLengths)
    if isnan(CollectionOfGridLengths(i)) 
        Weights(i)= RemainingSpaceRelative/NumverOfAutoSpacing;
    end
end

end

